<template>
	<view class="cs-note">
		<!-- tabs选项卡 -->
		<v-note-tabs @changeTabs='changeTabs'></v-note-tabs>

		<!-- 选项卡-笔记 -->
		<v-tabs-note v-if='tabsIndex === 0 && noteList.length > 0' :note_list='noteList' :is_longtap='isLongtap' :status="status" :content-text="contentText"
		 @longtapDelte='longtapDelte'></v-tabs-note>

		<!-- 选项卡-文件夹 -->
		<v-tabs-file v-if='tabsIndex === 1 && fileList.length > 0' :file_list='fileList' :status="status" :content-text="contentText"
		 @clickFileDelete='clickFileDelete'></v-tabs-file>

		<!-- 选项卡-试题笔记  -->
		<v-tabs-test v-if='tabsIndex === 2' :data_list='testList' :sid_list='sidList' :topicType="typeList" :status="status" :content-text="contentText"
		 @selectSid='selectSid' @selectTopic='selectQues' @changeDate='changeDate' @longtapDelte='longtapDelte'></v-tabs-test>

		<!-- 选项卡-试卷笔记 -->
		<v-tabs-exam v-if='tabsIndex === 3' :data_list='examList' :sid_list='sidList' :kindType="kindList" :status="status" :content-text="contentText"
		 @selectSid='selectSid' @selectTopic='selectKind' @changeDate='changeDate' @longtapDelte='longtapDelte'></v-tabs-exam>

		<!-- 选项卡-随堂笔记 -->
		<v-tabs-chapel v-if='tabsIndex === 4 && chapelList.length > 0' :chepel_list='chapelList' :is_longtap='isLongtap' :status="status" :content-text="contentText"
		 @longtapDelte='longtapDelte'></v-tabs-chapel>

		<!-- 底部操作 -->
		<v-opeartion v-if='btnArr.length > 0 && noteType == 1' :btn_arr='btnArr' @opearTion='opearTion'></v-opeartion>
		
		<!-- 无数据 -->
		<v-none v-if='(noteList.length <= 0  && tabsIndex === 0) || (fileList.length <= 0 && tabsIndex === 1) || (testList.length <= 0 && tabsIndex === 2)
		|| (examList.length <= 0 && tabsIndex === 3) || (chapelList.length <= 0 && tabsIndex === 4)' :none_msg='none_msg'></v-none>

		<!-- 删除弹窗 -->
		<v-prop v-if='isOpen' :msg='msg' @changeProp='changeProp'></v-prop>
	</view>
</template>

<script>
	import util from '@/common/util.js';
	import note_tabs from './components/Base-note-tabs'; //顶部选项卡
	import tabs_note from './component/tabs-note'; //普通笔记
	import tabs_file from './component/tabs-file'; //文件夹
	import tabs_test from './component/tabs-test'; //试题笔记
	import tabs_exam from './component/tabs-exam'; //试卷笔记
	import tabs_chapel from './component/tabs-chapel'; //随堂笔记
	import opeartion from './components/Base-opeartion'; //底部操作
	import none from './components/Base-none'; //无数据操作
	import prop from './components/Base-prop'; //删除弹框操作
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	export default {
		components: {
			vNoteTabs: note_tabs,
			vTabsNote: tabs_note,
			vTabsFile: tabs_file,
			vTabsTest: tabs_test,
			vTabsExam: tabs_exam,
			vTabsChapel: tabs_chapel,
			vOpeartion: opeartion,
			vNone: none,
			vProp: prop,
			uniLoadMore
		},
		data() {
			return {
				tabsIndex: uni.getStorageSync('tabsIndex') || 0, // tab选项索引值
				stuId: uni.getStorageSync('userInfo').id, //个人ID
				btnArr: [{
					title: '新建笔记',
					icon: 'icongengduocaozuo'
				}], // 默认渲染笔记的操作
				sidList: [], //科目列表
				typeList: [], //题型列表
				kindList: [], //试卷类型列表
				noteList: [], //笔记列表
				fileList: [], //文件夹列表
				testList: [], //试题笔记列表
				examList: [], //试卷笔记列表
				chapelList: [], //随堂笔记列表
				page: 1,
				reload: true,
				status: 'more',
				contentText: {
					contentdown: '上拉加载更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多',
					contentnot: "没数据"
				},
				none_msg: '您还没有上传~', //无数据弹窗提示内容
				sid: '', //科目ID
				quesId: '', //题型ID
				kindId: '', //试卷类型ID
				timeTxt: '', //所选时间
				isOpen: false, // 是否显示删除弹窗
				msg: '', // 删除弹窗提示信息
				isLongtap: false, // 控制长按删除时不能冒泡到点击事件
				listIndex: 0, //当前选中数据索引
				noteType: 1, //1家长 2孩子 
			}
		},
		onPullDownRefresh() {
			this.getDefault();
			this.getData();
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		onReachBottom() {
			this.status = 'loading';
			this.page = this.page + 1;
			var _this = this;
			setTimeout(function() {
				_this.getData()
			}, 500);
		},
		onLoad(e) {
			if(e.noteType) {
				this.noteType = e.noteType;
				this.stuId = uni.getStorageSync('currentChild').student_id;
				this.none_msg = '还没有上传内容~';
			}
			uni.setStorageSync('noteType',this.noteType);
			uni.setStorageSync('tabsIndex',0);
			this.getSubject();
			this.getKindType();
			this.getQuesType();
		},
		onShow() {
			this.tabsIndex = uni.getStorageSync('tabsIndex') || 0;
			this.getDefault();
			this.getData();
		},
		methods: {
			// 获取科目
			getSubject() {
				this.$zapi.apiPost('user/subject.Lists/subjectLists').then(res => {
					if(res.data.code == 200 && res.data.data.length > 0) {
						this.sidList = res.data.data;
						this.sidList.unshift({
							subject_name: '全部科目',
							sid: ''
						})
						this.sidList.forEach((item, index) => {
							this.sidList[index].title = item.subject_name
						})
					}
				})
			},
			// 获取试卷类型
			getKindType() {
				this.$zapi.apiPost('user/currency.basic/kindList').then(res => {
					if(res.data.code == 200 && res.data.data.length > 0) {
						this.kindList = res.data.data;
						this.kindList.unshift({
							kind_type_name: '全部类型',
							kind_type_id: ''
						})
						this.kindList.forEach((item, index) => {
							this.kindList[index].title = item.kind_type_name
							this.kindList[index].type_id = item.kind_type_id
						})
					}
				})
			},
			// 获取题目类型
			getQuesType() {
				this.$zapi.apiPost('user/currency.basic/questionTypeList').then(res => {
					if(res.data.code == 200 && res.data.data.length > 0) {
						this.typeList = res.data.data;
						this.typeList.unshift({
							question_type_name: '全部题型',
							question_type_id: ''
						})
						this.typeList.forEach((item, index) => {
							this.typeList[index].title = item.question_type_name
							this.typeList[index].type_id = item.question_type_id
						})
					}
				})
			},
			// 获取文件
			getData(url,data) {
				var urls = 'mobile/notes/infos';
				var result = {usr_id: this.stuId,page:this.page,limit:12}
				if(this.tabsIndex == 2 || this.tabsIndex == 3) {
					if(this.sid) {result.sid = this.sid;}
					if(this.timeTxt) {result.create_time = this.timeTxt;}
				}
				switch (this.tabsIndex){
					case 0: //查询笔记
						result.notes_type = 4;
						break;
					case 1: //查询文件夹
						urls = 'mobile/folder/infos';
						break;
					case 2: //查询试题
						result.notes_type = 1;
						if(this.quesId) {result.question_type_id = this.quesId;}
						break;
					case 3: //查询试卷
						result.notes_type = 2;
						if(this.kindId) {result.kind_type_id = this.kindId;}
						break;
					case 4: //查询随堂
						result.notes_type = 3;
						break;
					default:
						break;
				}
				this.$kbapi.apiPost(urls,result).then(res=>{
					this.getValue(res);
				})
			},
			// 公用获取数据
			getValue(res) {
				var _this = this;
				if(res.data.code == 200 && res.data.data.list.length > 0) {
					var list = res.data.data.list;
					list.forEach((item,key)=>{
						item.Time = item.create_time.substr(0,16);
					})
					if(_this.tabsIndex != 1) {
						// 给数据增加一个删除弹窗开关条件
						list.forEach((item, index) => {
							item['isOpen'] = false;
						})
					}
					switch (_this.tabsIndex){
						case 0: //查询笔记
							_this.noteList = _this.reload ? list : _this.noteList.concat(list);
							break;
						case 1: //查询文件夹
							_this.fileList = _this.reload ? list : _this.fileList.concat(list);
							break;
						case 2: //查询试题
							list.forEach((item, key) => {
								item['Cont'] = {};
							})
							var newArr = list.map(item => {
								return item.course_key
							})
							var question_id = [];
							for (var i = 0; i < newArr.length; i++) { //删除重复数据
							    if (question_id.indexOf(newArr[i]) == -1) {
									question_id.push(newArr[i]);
								}
							}
							_this.$zapi.apiPost('user/question.Lists/questionList', {
								question_id: question_id,
								pagesize: question_id.length,
								is_get_answer: 2
							}).then(result => {
								var data = result.data.data.data;
								// 格式化时间
								list.forEach((item, key) => {
									data.forEach((val, index) => {
										if(item.course_key == val.question_id) {
											item.Cont = val;
										}
									})
								})
							})
							_this.testList = _this.reload ? list : _this.testList.concat(list);
							_this.$forceUpdate();
							break;
						case 3: //查询试卷
							list.forEach((item, key) => {
								item['Cont'] = {};
								item.kind_type_name = util.getKindName(item.kind_type_id)
							})
							var newArr = list.map(item => {
								return item.course_key
							})
							var eids = [];
							for (var i = 0; i < newArr.length; i++) { //删除重复数据
							    if (eids.indexOf(newArr[i]) == -1) {
									eids.push(newArr[i]);
								}
							}
							_this.$zapi.apiPost('user/exam.Lists/examLists', {
								eid: eids,
								pagesize: eids.length
							}).then(result => {
								var data = result.data.data.data;
								// 格式化时间
								list.forEach((item, key) => {
									data.forEach((val, index) => {
										if(item.course_key == val.eid) {
											item.Cont = val;
										}
									})
								})
							})
							_this.examList = _this.reload ? list : _this.examList.concat(list);
							_this.$forceUpdate();
							break;
						case 4: //查询随堂
							_this.chapelList = _this.reload ? list : _this.chapelList.concat(list);
							break;
						default:
							break;
					}
					_this.reload = false;
					_this.status = 'more';
				} else {
					if (_this.reload) {
						_this.noteList = [];
						_this.fileList = [];
						_this.testList = [];
						_this.examList = [];
						_this.chapelList = [];
					} else {
						_this.status = 'noMore';
					}
				}
			},
			// 初始化数据
			getDefault() {
				this.page = 1;
				this.reload = true;
				this.status = 'more';
				this.noteList = []; //笔记列表
				this.fileList = []; //文件夹列表
				this.testList = []; //试题笔记列表
				this.examList = []; //试卷笔记列表
				this.chapelList = []; //随堂笔记列表
				this.btnArr = [];
				if(this.tabsIndex == 0) {
					this.btnArr = [{
						title: '新建笔记',
						icon: 'icongengduocaozuo'
					}]
				} else if(this.tabsIndex == 1) {
					this.btnArr = [{
						title: '新建文件夹',
						icon: 'icongengduocaozuo'
					}]
				}
			},
			// 选项卡切换
			changeTabs(index) {
				this.tabsIndex = index;
				uni.setStorageSync('tabsIndex',this.tabsIndex);
				this.getDefault();
				this.sid = ''; //科目ID
				this.quesId = ''; //题型ID
				this.kindId = ''; //试卷类型ID
				this.timeTxt = ''; //所选时间
				this.getData();
			},
			// 科目选择
			selectSid(sid) {
				this.getDefault();
				this.sid = sid;
				this.getData();
			},
			// 题型选择
			selectQues(question_typeId) {
				this.getDefault();
				this.quesId = question_typeId;
				this.getData();
			},
			// 试卷类型选择
			selectKind(type_id) {
				this.getDefault();
				this.kindId = type_id;
				this.getData();
			},
			// 时间选择
			changeDate(time) {
				this.getDefault();
				this.timeTxt = time;
				this.getData();
			},
			// 普通笔记/随堂笔记长按显示删除笔记弹窗
			longtapDelte(index) {
				if(this.noteType ==1) {
					this.msg = `删除之后无法恢复，您确定要删除此笔记？`
					this.isLongtap = true //将长按状态挂起来，避免触发点击事件
					this.isOpen = true //显示删除弹窗
					this.listIndex = index; //每次长按获取当前笔记索引
					if (this.tabsIndex === 0) {
						this.noteList[index].isOpen = !this.noteList[index].isOpen; // 显示/隐藏删除边框
					}
					if (this.tabsIndex === 2) {
						this.testList[index].isOpen = !this.testList[index].isOpen; // 显示/隐藏删除边框
					}
					if (this.tabsIndex === 3) {
						this.examList[index].isOpen = !this.examList[index].isOpen; // 显示/隐藏删除边框
					}
					if (this.tabsIndex === 4) {
						this.chapelList[index].isOpen = !this.chapelList[index].isOpen; // 显示/隐藏删除边框
					}
				}
			},
			// 文件夹删除事件显示删除弹框
			clickFileDelete(index) {
				this.listIndex = index // 获取文件索引
				this.msg = `删除之后无法恢复，您确定要删除此文件夹？`
				if(this.fileList[this.listIndex].notes_num > 0) {
					uni.showToast({
						title: '该文件夹下有笔记，请先清空该文件夹再进行删除',
						icon: 'none',
						duration: 2000
					})
				} else {
					this.isOpen = true // 显示弹窗
				}
			},
			// 删除弹框事件
			changeProp(isConfirm) {
				var arr = [];
				var url = 'mobile/notes/delete';
				switch (this.tabsIndex){
					case 0: //查询笔记
						arr = this.noteList;
						break;
					case 1: //查询笔记
						arr = this.fileList;
						url = 'mobile/folder/delete/'+arr[this.listIndex].unid
						break;
					case 2: //查询随堂
						arr = this.testList;
						break;
					case 3: //查询随堂
						arr = this.examList;
						break;
					case 4: //查询随堂
						arr = this.chapelList;
						break;
					default:
						break;
				}
				arr.forEach((item,index)=>{
					item.isOpen = false;
				})
				var unid = arr[this.listIndex].unid;
				if(isConfirm) {
					this.$kbapi.apiPost(url,{unid:unid,usr_id:this.stuId}).then(res=>{
						if(res.data.code == 200) {
							arr.splice(this.listIndex,1);
							this.$forceUpdate();
						} else {
							this.showToast(res.data.message);
						}
					})
				}
				this.isOpen = false; // 隐藏删除弹窗
				this.isLongtap = false; // 将长按状态取消
			},
			// 笔记 / 文件夹 底部操作
			opearTion(title) {
				switch (title) {
					case '新建笔记':
						uni.navigateTo({
							url: './component/tabs-note-create'
						})
						break;
					case '新建文件夹':
						uni.navigateTo({
							url: './component/tabs-file-new'
						})
						break;
				}
			},
			/* 提示方法 */
			showToast(title,icon,time) {
				var icons = 'none';
				var duration = 1500;
				if(icon) {
					icons = icon;
				}
				if(time) {
					duration = time;
				}
				uni.showToast({
					title: title,
					icon: icons,
					mask: true,
					duration: duration
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #FAFAFA;
	}

	.cs-note {
		overflow: auto;
	}
</style>
